package com.scrat.huzhenjie.service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scrat.huzhenjie.dao.HuzhenjieDao;
import com.scrat.huzhenjie.model.Test;
import com.scrat.huzhenjie.model.Users;

@Service(value = "huzhenjieService")
public class HuzhenjieService {
	@Autowired
	public HuzhenjieDao huzhenjieDao;
	protected static final Logger LOG = Logger.getLogger("huzhenjie");
	public Test testBAESQL(){
		return huzhenjieDao.testBAESQL();
	}
	
	private String md5Encoder(String str) throws NoSuchAlgorithmException{
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(str.getBytes());
		byte[] digesta = md5.digest();
		String result = "";
		for (byte b : digesta) {
			String stmp = Integer.toHexString(b & 0XFF);
			if (stmp.length() == 1) {
				result += "0" + stmp;
			} else {
				result += stmp;
			}
		}
		return result.toUpperCase();
	}
	
	public boolean login(String email , String password) {
		boolean result = false;
		try {
			password = md5Encoder(password);
			Users user = huzhenjieDao.getUsers(email);
			if (user.getPassword().equals(password)) {
				result = true;
			}
		} catch (Exception e) {
			LOG.log(Level.WARNING,e.toString());
		}
		return result;
	}
}
